package seqlist.stack_queue.leetcode;

import java.util.Stack;

/**
 * @author yuisama
 * @date 2023/03/21 19:55
 **/
public class Offer31_ValidateStackSequences {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        // 标识当前popped数组处理到哪个元素了~
        int i = 0;
        Stack<Integer> stack = new Stack<>();
        // 将push数组的元素依次入栈
        for (int val : pushed) {
            stack.push(val);
            while (!stack.isEmpty() && stack.peek() ==
            popped[i]) {
                stack.pop();
                i ++;
            }
        }
        return stack.isEmpty();
    }
}